/**
 * 第二场景 ScreenTwo
 */
class ScreenTwo extends egret.DisplayObjectContainer {
    /** 加载背景 */
    private bg_png = Main.createBitmapByName("bg_2_png");
    /** 加载角色 */
    private role_png = Main.createBitmapByName("2_0_png");
    /** 加载门 */
    private door = Main.createBitmapByName("door_png");
    /** 加载雨 */
    private rain_png = Main.createBitmapByName("rain1_png");
    /** 角色动画定时器 */
    private role_timer = new egret.Timer(1000 / 3, 3);
    /** 警察动画定时器 */
    private jc_timer = new egret.Timer(1000 / 3, 4);
    /** 创建提示点 */
    private tipPoint = Main.createTipPoint();

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        Main.createScale(this.bg_png, this);
        Main.createScale(this.role_png, this);
        Main.createScale(this.door, this);
        Main.createScale(this.rain_png, this);
        /** 添加提示点 */
        this.addChild(this.tipPoint);
        this.tipPoint.x = 720;
        this.tipPoint.y = 350;
        /** 设置门的属性 */
        this.door.anchorOffsetX = this.door.width
        this.door.scaleX = .5 * -1;
        this.door.scaleY = .5;
        this.door.x = 655;
        this.door.y = 210;
        this.door.touchEnabled = true;
        this.door.once(egret.TouchEvent.TOUCH_TAP, this.openTherDoor, this);
        /** 播放下雨动画 */
        this.rainAnimation();
        /** 添加标题 */
        // Main.screenTitle(this, "第二幕");
    }
    /** 下雨动画 */
    private rainAnimation() {
        const timer = new egret.Timer(1000 / 4);
        let num = 1;
        timer.addEventListener(egret.TimerEvent.TIMER, rainAnimationHandler, this);
        timer.start();
        /** 下雨动画处理器 */
        function rainAnimationHandler() {
            num++;
            if (num > 2) num = 1;
            this.rain_png.texture = RES.getRes(`rain${num}_png`);
        }
    }
    /** 开门 */
    private openTherDoor() {
        /** 去除提示点 */
        this.removeChild(this.tipPoint);
        /** 门完全打开后执行, 角色向屋内跑去 */
        egret.Tween.get(this.door).to({ x: this.door.x + this.door.width * .5 - 5 }, 1000).call(() => this.roleRun());
    }
    /** 角色向屋内跑去 */
    private roleRun() {
        const timer = new egret.Timer(1000 / 3, 3);
        this.role_png.texture = RES.getRes(`2_1_png`);
        const run_png = Main.createBitmapByName(`2_2_png`);
        Main.createScale(run_png, this);
        /** 移动雨云 */
        egret.Tween.get(this.rain_png).to({ x: this.rain_png.x - 200 }, 800);
        /** 帧动画设置 */
        let num = 2;
        timer.addEventListener(egret.TimerEvent.TIMER, animationHandler, this);
        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.jcRun, this);
        timer.start();
        /** 动画处理器 */
        function animationHandler() {
            num++;
            run_png.texture = RES.getRes(`2_${num}_png`);
        }
    }
    /** 警察跑来 */
    private jcRun() {
        /** 角色动画定时器 */
        const timer = new egret.Timer(1000 / 3, 4);
        const run_png = Main.createBitmapByName(`JC1_png`);
        this.addChild(run_png);

        run_png.anchorOffsetX = run_png.width;
        run_png.scaleX = -1;

        let num = 1;
        timer.addEventListener(egret.TimerEvent.TIMER, timerHandler, this);
        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, timerCompleteHandler, this);
        timer.start();

        function timerHandler() {
            num++;
            run_png.texture = RES.getRes(`JC${num}_png`);
        }
        function timerCompleteHandler() {
            run_png.x = run_png.x - 200;
            egret.Tween.get(this).wait(3000).call(() => {
                ScreenLayer.createAlert(this);
            })
        }

    }
}